import router from '../router'

const whiteList = ['/login', '/out','/type/list']
import {setLastPath, setTopUser, getLastPath} from "../util/cookie";
import {getToken} from "../util/cookie";

let LOGIN_URL = '/login'
import store from '../store'
import Wx from '@/wx'

router.beforeEach((to, from, next) => {
    //微信地址要获取签名用的
    Wx.setPath(to.fullPath)
    let topUserId = to.query[process.env.VUE_APP_TOP_USER_ID]
    if (topUserId) {//如果携带推荐人ID
        if(topUserId.indexOf("[")>-1){
            setTopUser(topUserId[0])
        }else {
            setTopUser("")
            setTopUser(topUserId)
        }
    }
    let token = store.state.user.token;
    //不是白名单
    if (whiteList.indexOf(to.path) < 0 && !token) {
        //记录LAST_PATH
        setLastPath(to.fullPath)
        //前往登录
        next(LOGIN_URL)
    } else {
        next()
    }


})